#include <stdio.h>
#include <string.h>

#define MAX_EXP 2000

void readPoly(int poly[]) {
    int n;
    scanf("%d", &n);
    memset(poly, 0, sizeof(int) * (MAX_EXP + 1));
    for (int i = 0; i < n; i++) {
        int coeff, exp;
        scanf("%d %d", &coeff, &exp);
        poly[exp] = coeff;
    }
}

void addPoly(int p1[], int p2[], int res[]) {
    memset(res, 0, sizeof(int) * (MAX_EXP + 1));
    for (int exp = 0; exp <= MAX_EXP; exp++) {
        res[exp] = p1[exp] + p2[exp];
    }
}

void multiplyPoly(int p1[], int p2[], int res[]) {
    memset(res, 0, sizeof(int) * (MAX_EXP + 1));
    for (int exp1 = 0; exp1 <= MAX_EXP; exp1++) {
        if (p1[exp1] == 0) continue;
        for (int exp2 = 0; exp2 <= MAX_EXP; exp2++) {
            if (p2[exp2] == 0) continue;
            int newExp = exp1 + exp2;
            if (newExp > MAX_EXP) continue;
            res[newExp] += p1[exp1] * p2[exp2];
        }
    }
}

void printPoly(int poly[]) {
    int count = 0;
    for (int exp = MAX_EXP; exp >= 0; exp--) {
        if (poly[exp] != 0) count++;
    }
    if (count == 0) {
        printf("0 0\n");
        return;
    }
    int printed = 0;
    for (int exp = MAX_EXP; exp >= 0; exp--) {
        if (poly[exp] != 0) {
            if (printed > 0) printf(" ");
            printf("%d %d", poly[exp], exp);
            printed++;
        }
    }
    printf("\n");
}

int main() {
    int p1[MAX_EXP + 1], p2[MAX_EXP + 1];
    int product[MAX_EXP + 1], sum[MAX_EXP + 1];

    readPoly(p1);
    readPoly(p2);

    multiplyPoly(p1, p2, product);
    addPoly(p1, p2, sum);

    printPoly(product);
    printPoly(sum);

    return 0;
}
